library ItemShiva initializer init requires LegacySystem

	globals
		private boolean SHIVA_USED = false
	endglobals
	
	private function XML9CShivaConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A01C'
	endfunction

	private function XML9CShiva takes nothing returns nothing
		
		local integer i
		
		local unit shivamissile
		local unit explosion
		local unit slowaura

		local unit caster = GetTriggerUnit()
		
		local player p = GetOwningPlayer(caster)

		local location caster_loc = GetUnitLoc(caster)
		local location target_loc = GetSpellTargetLoc() 

		local real x = GetLocationX(target_loc)
		local real y = GetLocationY(target_loc)
		local real x2 = GetLocationX(caster_loc)
		local real y2 = GetLocationY(caster_loc)
	   
		local real missiletime = (DistanceBetweenPoints(target_loc ,caster_loc)/1500.00)+0.2
		local rect r = Rect(x-800,y-800,x+800,y+800)
		local rect r2 = Rect(x-1800,y-1800,x+1800,y+1800)
		
		local unit slowdummy 
		
		if(missiletime<0) then 
			 set missiletime = missiletime*-1.0
		endif

		if not SHIVA_USED then
			set SHIVA_USED = true
			set i = 1
			call ALICEText(10.0, "A Nuclear Weapon was used on U.S. soil. |cffff000050 XP|r penalty to all Marines." )
			loop
                exitwhen i>udg_InitialPlayers
                if GetPlayerSlotState(ConvertedPlayer(udg_Sorted[i]))!=PLAYER_SLOT_STATE_LEFT then
                    set udg_Experience[udg_Sorted[i]] = udg_Experience[udg_Sorted[i]] - 50.0
                endif
                set i=i+1
            endloop
			set udg_XPFactor = udg_XPFactor * 0.95
		endif

		// Core
		//==================================================================

		// create nuke aura effect
		set explosion = CreateUnit(Player(11),'e00R',x,y,0.0) 
		if(missiletime > 1) then
		   call UnitApplyTimedLife(explosion, 'Bhwd', R2I(missiletime))
		else
		   call UnitApplyTimedLife(explosion, 'Bhwd', 1.0)
		endif

		// give sight
		set i = 1
		loop
			exitwhen i>udg_Players
			set explosion = CreateUnit(ConvertedPlayer(udg_Sorted[i]),'e002',x,y,0.0)
			call UnitApplyTimedLife(explosion,'Bhwd',27)
			set i=i+1
		endloop

		// ping for everyone
		call PingMinimapEx(x,y,10,255,0,0,false)

		// astalavista baby
		if GetRandomInt(0,2)==1 then
			set i = GetConvertedPlayerId(p)
			if GetRandomInt(0,1)==1 then
				call FieldChat(caster, 5.0, udg_Pcolors[i] + GetPlayerName(p) + "|r : Say hello to my little friend!" )
			else
				call FieldChat(caster, 5.0, udg_Pcolors[i] + GetPlayerName(p) + "|r : Hasta la vista, baby." )
			endif
		endif
		
		set shivamissile = CreateUnit(Player(11),'h00R',x2,y2,0.0)
		call IssuePointOrder(shivamissile,"attackground",x,y)

		call TriggerSleepAction(missiletime)
		call PingMinimapEx(x,y,5,255,0,0,false)
		call RemoveUnit(shivamissile)

		// damage
		
		set slowdummy = CreateUnit(Player(11),'e00I',x,y,0.0) 
		call UnitAddAbility(slowdummy, 'Aloc')
		call UnitAddAbility(slowdummy, 'A041')
			
		call UnitDamagePoint(caster,0,900.0,x,y,40000.0,true,false,ATTACK_TYPE_SIEGE,DAMAGE_TYPE_DEMOLITION,WEAPON_TYPE_WHOKNOWS)
		call DamageTrees(r2, 25000)
			
		// kill items,trees and set terrain to barren
		call EnumItemsInRect(r,null,function killitem)
		set udg_myinteger = 10
		call EnumDestructablesInRect(r,null,function killtree)
		call SetTerrainTypeBJ(target_loc,'Xdrt',-1,8,0)

		call StartSound(gg_snd_ShivaBoom)
		// create explosion

		// flash
		set explosion = CreateUnit(p,'e00V',x,y,0.0) 
		call UnitApplyTimedLife(explosion, 'Bhwd', 3)
		call SetUnitTimeScalePercent( explosion, 300.00)
		
		// explosion
		set explosion = CreateUnit(p,'e00K',x,y,0.0) 
		call UnitApplyTimedLife(explosion, 'Bhwd', 26)
		call SetUnitTimeScalePercent( explosion, 50.00 )
	   
		// shockwave
		set explosion = CreateUnit(p,'e00S',x,y,0.0) 
		call UnitApplyTimedLife(explosion, 'Bhwd', 8)
		call SetUnitAnimation( explosion, "birth" )
		  
		call TriggerSleepAction(2.5)

		// and one more damage
		call EnumItemsInRect(r,null,function killitem)
		call UnitDamagePoint(caster,0,900.0,x,y,35000.0,true,false,ATTACK_TYPE_SIEGE,DAMAGE_TYPE_DEMOLITION,WEAPON_TYPE_WHOKNOWS)
		
		call RemoveLocation(caster_loc)
		call RemoveLocation(target_loc)
		call RemoveRect(r)
		call RemoveRect(r2)
		call StartSound(gg_snd_Shiva)
	   
		call TriggerSleepAction(2.5)
		call RemoveUnit(slowdummy)
		
		// fallout 
		set explosion = CreateUnit(p,'e00U',x,y,0.0) 
		call UnitApplyTimedLife(explosion, 'Bhwd', 20)
		call SetUnitTimeScalePercent( explosion, 25.00 )
		
		set explosion = null
		set caster = null
		set shivamissile = null
		set slowaura = null
		set r = null
		set caster_loc = null
		set target_loc = null
		set p = null
		set slowdummy = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t,Condition(function XML9CShivaConditions))
		call TriggerAddAction(t,function XML9CShiva)
	endfunction

endlibrary